package com.jd.orm.framework.core.dao.impl;

import java.util.List;
import com.jd.orm.framework.core.dao.IBaseDao;
import com.jd.orm.framework.core.entity.BaseEntity;
import com.jd.orm.framework.core.exception.DbError;
import com.jd.orm.framework.core.exception.SystemException;
import com.jd.orm.framework.core.transaction.JdbcSession;
import com.jd.orm.framework.core.util.BeanSQLUtils;

public class DBTableUtils<T extends BaseEntity> {

	private IBaseDao<T> baseDao;

	/**
	 * @return the baseDao
	 */
	public IBaseDao<T> getBaseDao() {
		return baseDao;
	}

	/**
	 * @param baseDao
	 *            the baseDao to set
	 */
	public void setBaseDao(IBaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}

	/**
	 * 系统初始化批量创建表格
	 * 
	 * @param objs
	 */
	public void createTable(JdbcSession jdbcSession, List<Object> objs) {
		try {
			for (Object obj : objs) {
				BaseEntity baseEntity = (BaseEntity) obj;
				// SchemaInfo schemaInfo =BeanSQLUtils.getSchemaInfo(obj,
				// BeanSQLUtils.QUERY_TYPE);
				String createsql = BeanSQLUtils.get_sql_table_create(
						baseEntity.getSchemaInfo(),
						JdOrmTemplate.doGetDialet(jdbcSession.getConnection()));
				JdOrmTemplate.execute(jdbcSession.getConnection(), createsql,
						null);
			}
		} catch (Exception e) {
			throw SystemException.unchecked(e, DbError.IO_ERROR);
			// logger.error(e.getMessage());
		}
	}

	/**
	 * 删除表格
	 * 
	 * @param table
	 */
	public void dropTable(JdbcSession jdbcSession, String table) {
		try {
			String dropsql = JdOrmTemplate.doGetDialet(
					jdbcSession.getConnection()).sql_table_drop(table);
			JdOrmTemplate.execute(jdbcSession.getConnection(), dropsql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}

	}

	/**
	 * 重新命名
	 * 
	 * @param oldName
	 * @param newName
	 */
	public void renameTableColumnName(JdbcSession jdbcSession, String oldName,
			String newName) {
		try {
			String renameSql = JdOrmTemplate.doGetDialet(
					jdbcSession.getConnection()).sql_table_rename(oldName,
					newName);
			JdOrmTemplate.execute(jdbcSession.getConnection(), renameSql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}

	}

	/**
	 * 增加一列
	 * 
	 * @param table
	 * @param column_definition
	 * @param column_position
	 */
	public void addTableColumn(JdbcSession jdbcSession, String table,
			String column_definition, String column_position) {
		try {
			String addTableSql = JdOrmTemplate.doGetDialet(
					jdbcSession.getConnection()).sql_column_add(table,
					column_definition, column_position);
			JdOrmTemplate.execute(jdbcSession.getConnection(), addTableSql,
					null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}
	}

	/**
	 * 修改列名
	 * 
	 * @param table
	 * @param column_definition
	 * @param column_position
	 */
	public void modifyTableColumn(JdbcSession jdbcSession, String table,
			String column_definition, String column_position) {
		try {
			String modifyTableSql = JdOrmTemplate.doGetDialet(
					jdbcSession.getConnection()).sql_column_modify(table,
					column_definition, column_position);
			JdOrmTemplate.execute(jdbcSession.getConnection(), modifyTableSql,
					null);

		} catch (Exception e) {
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}
	}

	/**
	 * 删除 列名
	 * 
	 * @param table
	 * @param column
	 */
	public void dropTableColumn(JdbcSession jdbcSession, String table,
			String column) {
		try {
			String dropTableSql = JdOrmTemplate.doGetDialet(
					jdbcSession.getConnection()).sql_column_drop(table, column);
			JdOrmTemplate.execute(jdbcSession.getConnection(), dropTableSql,
					null);
		} catch (Exception e) {
			throw SystemException.unchecked(e, DbError.IO_ERROR);
		}
	}

}
